package com.itheima.controller;


import com.github.pagehelper.PageInfo;
import com.itheima.entity.Members;
import com.itheima.result.Result;
import com.itheima.service.MembersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员管理
 */
@RestController
@RequestMapping("members")
public class MembersController {

    @Autowired
    private MembersService membersService;

    /**
     * 分页查询
     *
     * @param pageNum  当前页码
     * @param pageSize 每页大小
     * @return PageInfo<Movies>
     */
    @GetMapping("list")
    public Result findByPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize,
            Integer cid,
            String keyword) {
        //调用service完成分页查询
        PageInfo<Members> list = membersService.findByPage(pageNum, pageSize, cid, keyword);
        return Result.success(list);
    }


    /**
     * 查询所有
     *
     * @return List<Members>
     */
    @GetMapping("all")
    public Result findAll() {
        //调用service完成分页查询
        List<Members> list = membersService.findAll();
        return Result.success(list);
    }

    /**
     * 根据id查询
     *
     * @param id 主键
     * @return movies
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam Integer id) {
        Members members = membersService.findById(id);
        return Result.success(members);
    }

    /**
     * 新增或修改
     *
     * @param members 电影对象
     * @return 无
     */
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody Members members) {
        if (members.getId() == null) {
            // 新增
            membersService.save(members);
        } else {
            // 修改
            membersService.update(members);
        }
        return Result.success();
    }

    /**
     * 根据id删除
     *
     * @param id 主键
     * @return 无
     */
    @DeleteMapping("/deleteById")
    public Result deleteById(@RequestParam Integer id) {
        membersService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     *
     * @param ids 主键数组
     * @return 无
     */
    @DeleteMapping("/deleteByIds")
    public Result deleteByIds(Integer[] ids) {
        membersService.deleteBatch(ids);
        return Result.success();
    }
}